#include "debug.h"
#include "iot2.h"
#include "controller.h"
#include "FreeRTOS.h"
#include "task.h"

/* Global define */
// 当配180byte时, STK最低的剩余值: 56Byte. 最多使用126byte.
// 当配180byte时, 去除output_str相关变量,STK最低的剩余值:138 byte. 最多使用42byte(其中4byte系count).
// 其实, 核心原因都在:OLED_ShowString()
#define TASK1_TASK_PRIO     5
#define TASK1_STK_SIZE      128

// 当配256byte时, STK最低的剩余值:Byte. 最多使用115byte.
#define IOT_TASK_PRIO       5
#define IOT_TASK_STK_SIZE   165

// 配256byte时, 剩134byte.最多使用:122byte
#define IOT_MONITOR_PRIO  5
#define IOT_MONITOR_STK_SIZE  256

#define SERVICE_TASK_PRIO  5
#define SERVICE_TASK_STK_SIZE 256


/* Global Variable */
TaskHandle_t Task1Task_Handler;
TaskHandle_t iot_task_Handler;
TaskHandle_t Task3Task_Handler;
TaskHandle_t iot_monitor_task_Handler;
TaskHandle_t service_task_Handler;
/* Global Variable */


/*********************************************************************
 * @fn      GPIO_Toggle_INIT
 *
 * @brief   Initializes GPIOA.0/1
 *
 * @return  none
 */
void GPIO_Toggle_INIT(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure={0};

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  GPIO_Init(GPIOE, &GPIO_InitStructure);
  
}

void task1_task(void *pvParameters)
{
    uint16_t k = 0x04, tmp = 0;
    printf("task1 entry\r\n");
    while(1)
    {
        tmp = GPIO_ReadOutputData(GPIOE);
        GPIO_Write(GPIOE, (tmp | 0x3CU) & ~k);
        if (k >= 0x08) {
            k = 0x04;
        } else {
            k <<= 1;
        }
        vTaskDelay(50);
    }
}

int main(void)
{
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    SystemCoreClockUpdate();
    Delay_Init();
    USART_Printf_Init(115200);

    printf("SystemClk:%d\r\n", SystemCoreClock);
    printf("ChipID:%08x\r\n", DBGMCU_GetCHIPID());
    printf("FreeRTOS Kernel Version:%s\r\n", tskKERNEL_VERSION_NUMBER);

    GPIO_Toggle_INIT();
    iot_hal_init();

    xTaskCreate((TaskFunction_t)task1_task,
                (const char *)"task1",
                (uint16_t)TASK1_STK_SIZE,
                (void *)NULL,
                (UBaseType_t)TASK1_TASK_PRIO,
                (TaskHandle_t *)&Task1Task_Handler);

    xTaskCreate((TaskFunction_t)iot_task_entry,
                (const char *)"iot_task",
                (uint16_t)IOT_TASK_STK_SIZE,
                (void *)NULL,
                (UBaseType_t)IOT_TASK_PRIO,
                (TaskHandle_t *)&iot_task_Handler);

    xTaskCreate((TaskFunction_t)iot_monitor_task_entry,
                (const char *)"iot_monitor_task",
                (uint16_t)IOT_MONITOR_STK_SIZE,
                (void *)NULL,
                (UBaseType_t)IOT_MONITOR_PRIO,
                (TaskHandle_t *)&iot_monitor_task_Handler);

    xTaskCreate((TaskFunction_t)service_task_entry,
                (const char *)"service_task",
                (uint16_t)SERVICE_TASK_STK_SIZE,
                (void *)NULL,
                (UBaseType_t)SERVICE_TASK_PRIO,
                (TaskHandle_t *)&service_task_Handler);

    vTaskStartScheduler();

	while(1)
	{
	    printf("shouldn't run at here!!\n");
	}
}
